package System.Task;

import patterns.command.*;

import java.util.Scanner;

public class TaskExecutor  {
    private TaskManager taskManager = new TaskManager();

    public void showMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\n=== 任务执行与控制 ===");
            System.out.println("1. 创建新任务");
            System.out.println("2. 查看任务列表");
            System.out.println("3. 查看任务详情");
            System.out.println("4. 开始任务");
            System.out.println("5. 暂停任务");
            System.out.println("6. 发送控制指令");
            System.out.println("7. 处理紧急情况");
            System.out.println("8. 撤销上一步操作");
            System.out.println("0. 返回上级菜单");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    createTask(scanner);
                    break;
                case 2:
                    taskManager.displayTaskList();
                    break;
                case 3:
                    viewTaskDetails(scanner);
                    break;
                case 4:
                    startTask(scanner);
                    break;
                case 5:
                    pauseTask(scanner);
                    break;
                case 6:
                    sendControlCommand(scanner);
                    break;
                case 7:
                    handleEmergency(scanner);
                    break;
                case 8:
                    taskManager.undoLastCommand();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private void createTask(Scanner scanner) {
        System.out.println("输入任务描述:");
        String description = scanner.nextLine();

        System.out.println("输入农机ID:");
        String machineId = scanner.nextLine();

        System.out.println("输入农田ID:");
        String fieldId = scanner.nextLine();

        System.out.println("选择优先级: 1.低 2.正常 3.高 4.紧急");
        int priorityChoice = scanner.nextInt();
        TaskPriority priority = TaskPriority.NORMAL;

        switch (priorityChoice) {
            case 1: priority = TaskPriority.LOW; break;
            case 2: priority = TaskPriority.NORMAL; break;
            case 3: priority = TaskPriority.HIGH; break;
            case 4: priority = TaskPriority.EMERGENCY; break;
        }

        taskManager.createTask(description, machineId, fieldId, priority);
    }

    private void viewTaskDetails(Scanner scanner) {
        System.out.println("输入任务ID:");
        String taskId = scanner.nextLine();

        Task task = taskManager.getTask(taskId);
        if (task == null) {
            System.out.println("未找到该任务");
            return;
        }

        task.display();
    }

    private void startTask(Scanner scanner) {
        System.out.println("输入任务ID:");
        String taskId = scanner.nextLine();

        Task task = taskManager.getTask(taskId);
        if (task == null) {
            System.out.println("未找到该任务");
            return;
        }

        taskManager.executeCommand(new StartTaskCommand(task));
    }

    private void pauseTask(Scanner scanner) {
        System.out.println("输入任务ID:");
        String taskId = scanner.nextLine();

        Task task = taskManager.getTask(taskId);
        if (task == null) {
            System.out.println("未找到该任务");
            return;
        }

        taskManager.executeCommand(new PauseTaskCommand(task));
    }

    private void sendControlCommand(Scanner scanner) {
        System.out.println("输入任务ID:");
        String taskId = scanner.nextLine();

        Task task = taskManager.getTask(taskId);
        if (task == null) {
            System.out.println("未找到该任务");
            return;
        }
        System.out.println("输入控制指令:");
        String command = scanner.nextLine();

        taskManager.executeCommand(new SendControlCommand(task, command));
    }

    private void handleEmergency(Scanner scanner) {
        System.out.println("输入任务ID:");
        String taskId = scanner.nextLine();

        System.out.println("选择紧急类型: 1.低油量 2.碰撞风险 3.系统故障");
        int type = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        String emergencyType;
        switch (type) {
            case 1: emergencyType = "LOW_FUEL"; break;
            case 2: emergencyType = "COLLISION_RISK"; break;
            case 3: emergencyType = "SYSTEM_FAILURE"; break;
            default:
                System.out.println("无效类型");
                return;
        }

        System.out.println("输入紧急情况详情:");
        String details = scanner.nextLine();

        taskManager.handleEmergency(emergencyType, taskId, details);
    }
}

